﻿package com.amcharts.chartClasses
{
    import com.amcharts.*;
    import com.amcharts.axes.*;
    import com.amcharts.events.*;
    import flash.events.*;
    import mx.collections.*;
    import mx.styles.*;

    public class ChartScrollbar extends SimpleChartScrollbar
    {
        protected var _endTime:Number;
        protected var _timeDifference:Number;
        protected var _min:Number;
        protected var _end:Number;
        protected var _categoryAxis:CategoryAxis;
        protected var _firstTime:Number;
        protected var _baseY:Number;
        protected var _max:Number;
        protected var _stepHeight:Number;
        protected var _valueAxis:ValueAxis;
        protected var _graphType:String;
        protected var _unselectedGraph:AmGraph;
        protected var _mouseWheelStep:Number;
        protected var _graph:AmGraph;
        protected var _startTime:Number;
        protected var _stepWidth:Number;
        protected var _difference:uint;
        protected var _chart:AmSerialChart;
        protected var _start:Number;
        protected var _dataChanged:Boolean;
        protected var _selectedGraph:AmGraph;
        static const VERSION:String = "1.8.3.1";
        private static var stylesInited:Boolean = initStyles();

        public function ChartScrollbar() : void
        {
            _dataProvider = new ArrayCollection();
            return;
        }// end function

        override protected function createScrollbar() : void
        {
            super.createScrollbar();
            _valueAxis.width = this.width;
            _valueAxis.height = this.height;
            _valueAxis.setStyle("autoGridCount", false);
            _categoryAxis.width = this.width;
            _categoryAxis.height = this.height;
            _categoryAxis.rotate = _rotate;
            _categoryAxis.visibleAxisWidth = this.width;
            _categoryAxis.visibleAxisHeight = this.height;
            _categoryAxis.dateFormats = _chart.categoryAxis.dateFormats;
            var _loc_1:* = this.getStyle("autoGridCount");
            var _loc_2:* = this.getStyle("gridCount");
            if (_loc_2 <= 0)
            {
            }
            if (_loc_1)
            {
                _categoryAxis.setStyle("gridCount", this.getStyle("gridCount"));
                _categoryAxis.setStyle("autoGridCount", _loc_1);
                _categoryAxis.setStyle("gridColor", this.getStyle("gridColor"));
                _categoryAxis.setStyle("gridAlpha", this.getStyle("gridAlpha"));
                _categoryAxis.setStyle("tickLength", 0);
                _categoryAxis.inside = true;
            }
            else
            {
                _categoryAxis.visible = false;
            }
            if (_graph)
            {
                _selectedGraph.width = this.width;
                _selectedGraph.height = this.height;
                _unselectedGraph.width = this.width;
                _unselectedGraph.height = this.height;
            }
            zoomScrollbar();
            return;
        }// end function

        protected function zoomScrollbar() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            if (_categoryAxis.parseDates)
            {
            }
            if (!_categoryAxis.equalSpacing)
            {
                _categoryAxis.timeZoom(_dataProvider.getItemAt(0).time, _dataProvider.getItemAt((_dataProvider.length - 1)).time);
                _categoryAxis.validateNow();
                _firstTime = _dataProvider.getItemAt(0).time;
                _loc_3 = _dataProvider.getItemAt((_dataProvider.length - 1)).time;
                _stepWidth = _categoryAxis.stepWidth;
                _loc_1 = _stepWidth * (_startTime - _firstTime);
                _loc_2 = _stepWidth * (_endTime - _firstTime + _categoryAxis.minDuration);
            }
            else
            {
                _categoryAxis.validateNow();
                _loc_1 = _dataProvider.getItemAt(_start).x[_categoryAxis.id];
                _loc_2 = _dataProvider.getItemAt(_end).x[_categoryAxis.id];
                _stepWidth = _categoryAxis.stepWidth;
                if (!_categoryAxis.startOnAxis)
                {
                    _loc_4 = _stepWidth / 2;
                    _loc_1 = _loc_1 - _loc_4;
                    _loc_2 = _loc_2 + _loc_4;
                }
            }
            updateScrollbarSize(_loc_1, _loc_2);
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            _valueAxis = new ValueAxis();
            _valueAxis.axisItemRenderer = RectangularAxisItemRenderer;
            _valueAxis.axisRenderer = RectangularAxisRenderer;
            _valueAxis.guideFillRenderer = RectangularAxisGuideFillRenderer;
            _valueAxis.visible = false;
            _valueAxis.scrollbar = true;
            addChild(_valueAxis);
            _categoryAxis = new CategoryAxis();
            _categoryAxis.axisItemRenderer = RectangularAxisItemRenderer;
            _categoryAxis.axisRenderer = RectangularAxisRenderer;
            _categoryAxis.guideFillRenderer = RectangularAxisGuideFillRenderer;
            _categoryAxis.position = "bottom";
            _categoryAxis.mouseEnabled = false;
            _categoryAxis.setStyle("axisAlpha", 0);
            _unselectedGraph = new AmGraph();
            _unselectedGraph.scrollbar = true;
            addChild(_unselectedGraph);
            _selectedGraph = new AmGraph();
            _selectedGraph.scrollbar = true;
            _maskedArea.addChild(_selectedGraph);
            _categoryAxis.id = "scrollbar";
            addChild(_categoryAxis);
            this.visible = false;
            return;
        }// end function

        protected function updateData() : void
        {
            var _loc_4:Object = null;
            var _loc_5:String = null;
            var _loc_6:Number = NaN;
            _valueAxis.chart = _graph.chart;
            _valueAxis.id = _graph.valueAxis.id;
            _valueAxis.dataProvider = _dataProvider;
            _valueAxis.rotate = _rotate;
            _valueAxis.reversed = _graph.valueAxis.reversed;
            _valueAxis.logarithmic = _graph.valueAxis.logarithmic;
            _valueAxis.zoom(0, (_dataProvider.length - 1));
            var _loc_1:* = Infinity;
            var _loc_2:* = -Infinity;
            var _loc_3:Number = 0;
            while (_loc_3 < _dataProvider.length)
            {
                
                _loc_4 = _dataProvider.getItemAt(_loc_3).charts[_graph.chart.id].axes[_graph.valueAxis.id].graphs[_graph.id].values;
                for (_loc_5 in _loc_4)
                {
                    
                    if (_loc_5 != "percents")
                    {
                    }
                    if (_loc_5 != "total")
                    {
                        _loc_6 = _loc_4[_loc_5];
                        if (_loc_6 < _loc_1)
                        {
                            _loc_1 = _loc_6;
                        }
                        if (_loc_6 > _loc_2)
                        {
                            _loc_2 = _loc_6;
                        }
                    }
                }
                _loc_3 = _loc_3 + 1;
            }
            if (_loc_1 != Infinity)
            {
                _valueAxis.min = _loc_1;
            }
            if (_loc_2 != -Infinity)
            {
                _valueAxis.max = _loc_2 + (_loc_2 - _loc_1) * 0.1;
            }
            _unselectedGraph.id = _graph.id;
            _unselectedGraph.chart = undefined;
            _unselectedGraph.chartType = "serial";
            _unselectedGraph.valueAxis = _valueAxis;
            _unselectedGraph.chart = _graph.chart;
            _unselectedGraph.categoryAxis = _categoryAxis;
            _unselectedGraph.chartDataProvider = _dataProvider;
            _unselectedGraph.valueField = _graph.valueField;
            _unselectedGraph.openField = _graph.openField;
            _unselectedGraph.closeField = _graph.closeField;
            _unselectedGraph.highField = _graph.highField;
            _unselectedGraph.lowField = _graph.lowField;
            _unselectedGraph.rotate = _rotate;
            _unselectedGraph.setStyle("lineAlpha", getStyle("graphLineAlpha"));
            _unselectedGraph.setStyle("lineColor", getStyle("graphLineColor"));
            _unselectedGraph.setStyle("fillAlphas", [getStyle("graphFillAlpha")]);
            _unselectedGraph.setStyle("fillColors", [getStyle("graphFillColor")]);
            _unselectedGraph.setStyle("connect", _graph.getStyle("connect"));
            _unselectedGraph.mouseEnabled = false;
            _unselectedGraph.mouseChildren = false;
            _unselectedGraph.hidden = _graph.hidden;
            _selectedGraph.id = _graph.id;
            _selectedGraph.chart = undefined;
            _selectedGraph.chartType = "serial";
            _selectedGraph.valueAxis = _valueAxis;
            _selectedGraph.chart = _graph.chart;
            _selectedGraph.categoryAxis = _categoryAxis;
            _selectedGraph.chartDataProvider = _dataProvider;
            _selectedGraph.valueField = _graph.valueField;
            _selectedGraph.openField = _graph.openField;
            _selectedGraph.closeField = _graph.closeField;
            _selectedGraph.highField = _graph.highField;
            _selectedGraph.lowField = _graph.lowField;
            _selectedGraph.rotate = _rotate;
            _selectedGraph.setStyle("lineAlpha", getStyle("selectedGraphLineAlpha"));
            _selectedGraph.setStyle("lineColor", getStyle("selectedGraphLineColor"));
            _selectedGraph.setStyle("fillAlphas", [getStyle("selectedGraphFillAlpha")]);
            _selectedGraph.setStyle("fillColors", [getStyle("selectedGraphFillColor")]);
            _selectedGraph.setStyle("connect", _graph.getStyle("connect"));
            _selectedGraph.mouseEnabled = false;
            _selectedGraph.mouseChildren = false;
            _selectedGraph.hidden = _graph.hidden;
            if (_graphType)
            {
                _selectedGraph.type = _graphType;
                _unselectedGraph.type = _graphType;
            }
            else
            {
                _unselectedGraph.type = _graph.type;
                _selectedGraph.type = _graph.type;
            }
            _unselectedGraph.zoom(0, (_dataProvider.length - 1));
            _selectedGraph.zoom(0, (_dataProvider.length - 1));
            return;
        }// end function

        public function set graph(param1:AmGraph) : void
        {
            _graph = param1;
            _dataChanged = true;
            invalidateProperties();
            invalidateDisplayList();
            return;
        }// end function

        public function get graphType() : String
        {
            return _graphType;
        }// end function

        override protected function dispatchScrollbarEvent() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            var _loc_7:SerialChartEvent = null;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_11:Number = NaN;
            var _loc_12:Number = NaN;
            var _loc_3:* = _dragger.x;
            var _loc_4:* = _dragger.y;
            var _loc_5:* = _dragger.width;
            var _loc_6:* = _dragger.height;
            if (_rotate)
            {
                _loc_1 = _loc_4;
                _loc_2 = _loc_6;
            }
            else
            {
                _loc_1 = _loc_3;
                _loc_2 = _loc_5;
            }
            if (_categoryAxis.parseDates)
            {
            }
            if (!_categoryAxis.equalSpacing)
            {
                _loc_8 = Math.round(_loc_1 / _stepWidth) + _firstTime;
                if (!_dragging)
                {
                    _loc_9 = Math.round((_loc_1 + _loc_2) / _stepWidth) + _firstTime - _categoryAxis.minDuration;
                }
                else
                {
                    _loc_9 = _loc_8 + _timeDifference;
                }
                if (_loc_8 > _loc_9)
                {
                    _loc_8 = _loc_9;
                }
                if (_loc_8 == _startTime)
                {
                }
                if (_loc_9 != _endTime)
                {
                    _startTime = _loc_8;
                    _endTime = _loc_9;
                    _loc_7 = new SerialChartEvent(SerialChartEvent.ZOOMED);
                    _loc_7.start = _startTime;
                    _loc_7.end = _endTime;
                    _loc_7.startDate = new Date(_startTime);
                    _loc_7.endDate = new Date(_endTime);
                    dispatchEvent(_loc_7);
                }
            }
            else
            {
                if (!_categoryAxis.startOnAxis)
                {
                    _loc_12 = _stepWidth / 2;
                    _loc_1 = _loc_1 + _loc_12;
                    _loc_2 = _loc_2 - _loc_12;
                }
                _loc_10 = _categoryAxis.xToIndex(_loc_1);
                _loc_11 = _categoryAxis.xToIndex(_loc_1 + _loc_2);
                if (_loc_10 == _start)
                {
                }
                if (_end != _loc_11)
                {
                    if (_categoryAxis.startOnAxis)
                    {
                        if (_resizingRight)
                        {
                        }
                        if (_loc_10 == _loc_11)
                        {
                            _loc_11 = _loc_11 + 1;
                        }
                        if (_resizingLeft)
                        {
                        }
                        if (_loc_10 == _loc_11)
                        {
                            if (_loc_10 > 0)
                            {
                                _loc_10 = _loc_10 - 1;
                            }
                            else
                            {
                                _loc_11 = 1;
                            }
                        }
                    }
                    _start = _loc_10;
                    if (!_dragging)
                    {
                        _end = _loc_11;
                    }
                    else
                    {
                        _end = _start + _difference;
                    }
                    if (_end > (_dataProvider.length - 1))
                    {
                        _end = _dataProvider.length - 1;
                    }
                    _loc_7 = new SerialChartEvent(SerialChartEvent.ZOOMED);
                    _loc_7.start = _start;
                    _loc_7.end = _end;
                    if (_categoryAxis.parseDates)
                    {
                        _loc_7.startDate = new Date(_dataProvider.getItemAt(_start).time);
                        _loc_7.endDate = new Date(_dataProvider.getItemAt(_end).time);
                    }
                    dispatchEvent(_loc_7);
                }
            }
            return;
        }// end function

        public function set mainCategoryAxis(param1:CategoryAxis) : void
        {
            _categoryAxis.parseDates = param1.parseDates;
            _categoryAxis.equalSpacing = param1.equalSpacing;
            _categoryAxis.minPeriod = param1.minPeriod;
            _categoryAxis.firstDayOfWeek = param1.firstDayOfWeek;
            _categoryAxis.startOnAxis = param1.startOnAxis;
            return;
        }// end function

        public function get mouseWheelStep() : Number
        {
            return _mouseWheelStep;
        }// end function

        override protected function handleBackgroundClick(event:MouseEvent) : void
        {
            super.handleBackgroundClick(event);
            if (!_dragging)
            {
                _difference = _end - _start;
                _timeDifference = _endTime - _startTime;
                if (_timeDifference < 0)
                {
                    _timeDifference = 0;
                }
                if (_updateOnReleaseOnly)
                {
                    updateScrollbar();
                    dispatchScrollbarEvent();
                }
            }
            return;
        }// end function

        public function timeZoom(param1:Number, param2:Number) : void
        {
            _startTime = param1;
            _endTime = param2;
            _timeDifference = _endTime - _startTime;
            zoomScrollbar();
            return;
        }// end function

        public function set mouseWheelStep(param1:Number) : void
        {
            _mouseWheelStep = param1;
            return;
        }// end function

        public function set chart(param1:AmSerialChart) : void
        {
            _chart = param1;
            return;
        }// end function

        override protected function handleDragStart(event:MouseEvent) : void
        {
            super.handleDragStart(event);
            _difference = _end - _start;
            _timeDifference = _endTime - _startTime;
            if (_timeDifference < 0)
            {
                _timeDifference = 0;
            }
            return;
        }// end function

        public function get categoryAxis() : CategoryAxis
        {
            return _categoryAxis;
        }// end function

        public function zoom(param1:Number, param2:Number) : void
        {
            _start = param1;
            _end = param2;
            zoomScrollbar();
            return;
        }// end function

        public function set graphType(param1:String) : void
        {
            _graphType = param1;
            invalidateDisplayList();
            return;
        }// end function

        override public function set dataProvider(param1:ArrayCollection) : void
        {
            _dataProvider = param1;
            _dataChanged = true;
            _categoryAxis.dataProvider = _dataProvider;
            _categoryAxis.zoom(0, (_dataProvider.length - 1));
            invalidateDisplayList();
            return;
        }// end function

        override protected function updateDisplayList(param1:Number, param2:Number) : void
        {
            if (this.height)
            {
            }
            if (this.width)
            {
                _hideResizeGrips = this.getStyle("hideResizeGrips");
                if (_dataChanged)
                {
                }
                if (_graph)
                {
                    updateData();
                    _dataChanged = false;
                }
                if (_hideResizeGrips)
                {
                    _dragIconLeft.visible = false;
                    _dragIconRight.visible = false;
                }
                if (_dataProvider.length > 0)
                {
                    createScrollbar();
                    this.visible = true;
                }
                else
                {
                    this.visible = false;
                }
            }
            return;
        }// end function

        private static function initStyles() : Boolean
        {
            var styleDeclaration:* = StyleManager.getStyleDeclaration("ChartScrollbar");
            if (!styleDeclaration)
            {
                styleDeclaration = new CSSStyleDeclaration();
            }
            styleDeclaration.defaultFactory = function () : void
            {
                this.graphLineColor = 0;
                this.graphLineAlpha = 0;
                this.graphFillColor = 0;
                this.graphFillAlpha = 0.1;
                this.selectedGraphLineColor = 0;
                this.selectedGraphLineAlpha = 0;
                this.selectedGraphFillColor = 0;
                this.selectedGraphFillAlpha = 0.5;
                this.gridCount = 0;
                this.gridColor = 16777215;
                this.gridAlpha = 0.7;
                this.autoGridCount = false;
                return;
            }// end function
            ;
            StyleManager.setStyleDeclaration("ChartScrollbar", styleDeclaration, false);
            return true;
        }// end function

    }
}
